package com.vhr.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vhr.VO.CheckCountVO;
import com.vhr.VO.CheckVO;
import com.vhr.entities.Account;
import com.vhr.entities.CheckExtra;
import com.vhr.entities.MyPageInfo;
import com.vhr.entities.Result;
import com.vhr.service.AccountService;
import com.vhr.service.ICheckService;
import com.vhr.service.IOvertimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api(value = "CheckController", description = "出勤模块")
@Controller
@CrossOrigin
@RestController
public class CheckController {
    @Autowired
    public ICheckService iCheckService;
    @Autowired
    public AccountService accountService;
    @Autowired
    public IOvertimeService iOvertimeService;


    private List<CheckVO> checkVOList = null;

    public List<CheckVO> dataFormCheck(List<CheckExtra> checkList){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        List<CheckVO> checkVOList1 = new ArrayList<>();
        for(CheckExtra list : checkList){
            CheckVO checkVO = new CheckVO();
            checkVO.setCheckId(list.getCheckId());
            checkVO.setCurrentDate(sdf1.format(list.getCurrentDate()));
            if (list.getSgininTime() != null)
                checkVO.setSgininTime(sdf.format(list.getSgininTime()));
            else
                checkVO.setSgininTime(null);
            if (list.getSignbackTime() != null)
                checkVO.setSignbackTime(sdf.format(list.getSignbackTime()));
            else
                checkVO.setSignbackTime(null);
            checkVO.setEmpId(list.getEmpId());
            Account account = accountService.selectEmployeeByEmpId(list.getEmpId());
            if(account == null)
                checkVO.setEmpNum(null);
            else
                checkVO.setEmpNum(account.getEmpNum());
            checkVO.setCheckStatus(list.getCheckStatus());
            checkVO.setEmpName(list.getEmpName());
            checkVO.setDepartment(list.getDepartment());
            checkVOList1.add(checkVO);
        }
        return checkVOList1;
    }

    public <T> MyPageInfo myPageInfo(PageInfo pageInfo, List<CheckVO> checkVOList){
        MyPageInfo<CheckVO> myPageInfo = new MyPageInfo<>();
        myPageInfo.setList(checkVOList);
        myPageInfo.setPageNum(pageInfo.getPageNum());
        myPageInfo.setPageSize(pageInfo.getPageSize());
        myPageInfo.setRecordCount((int)pageInfo.getTotal());
        myPageInfo.setTotalPage(pageInfo.getPages());
        return myPageInfo;
    }

    //    //管理员查看所有出勤信息
    @GetMapping("/api/check/listAllCheck")
    @ResponseBody
    @ApiOperation("管理员查看所有出勤信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pn", paramType = "query", value = "页码", dataType = "Integer"),
    })
    public Result listAllCheck(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 7);
        List<CheckExtra> checkList = iCheckService.selectAllCheckWithName();
        if(checkList.size() == 0)
            return Result.fail();

        PageInfo pageInfo = new PageInfo(checkList, 3);
        checkVOList = dataFormCheck(checkList);
        MyPageInfo<CheckVO> myPageInfo =  myPageInfo(pageInfo, checkVOList);
        return Result.success().add("myPageInfo", myPageInfo);

    }

    //管理员按时间段查询出勤信息
    @GetMapping("/api/check/listCheckByDate")
    @ResponseBody
    @ApiOperation("管理员按时间段查询出勤信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "currentDate", paramType = "query", required = true, value = "起始日期", dataType = "Date"),
//            @ApiImplicitParam(name = "leaveEnd", paramType = "query", required = true, value = "结束日期", dataType = "Date"),
            @ApiImplicitParam(name = "pn", paramType = "query", value = "页码", dataType = "Integer")
    })
    public Result listCheckByDate(@RequestParam(value = "currentDate", required = true) Date currentDate, @RequestParam(value = "pn", defaultValue = "1")Integer pn){
        PageHelper.startPage(pn, 7);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String date = format.format(currentDate);
        List<CheckExtra> checkList= iCheckService.selectCheckByDate(date.substring(0,4), date.substring(5));
        if(checkList.size() == 0)
            return Result.fail();
        PageInfo pageInfo = new PageInfo(checkList, 3);
        checkVOList = dataFormCheck(checkList);
        MyPageInfo<CheckVO> myPageInfo =  myPageInfo(pageInfo, checkVOList);
        return Result.success().add("myPageInfo", myPageInfo);
    }


    //    管理员条件查询出勤信息
    @GetMapping("/api/check/listCheckByCondition")
    @ResponseBody
    @ApiOperation("管理员条件查询出勤信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", paramType = "query", value = "部门id", dataType = "Integer"),
            @ApiImplicitParam(name = "empName", paramType = "query", value = "员工姓名", dataType = "String"),
            @ApiImplicitParam(name = "status", paramType = "query", value = "出勤状态", dataType = "Integer"),
            @ApiImplicitParam(name = "currentDate", paramType = "query", value = "日期", dataType = "Date"),
            @ApiImplicitParam(name = "pn", paramType = "query", value = "页码", dataType = "Integer"),

    })
    public Result listCheckByCondition(@RequestParam(value = "deptId", defaultValue = "0") Integer deptId, @RequestParam(value = "empName", defaultValue = "%%", required = false)String empName, @RequestParam(value = "status", defaultValue = "-1")Integer status, @RequestParam(value = "currentDate", required = false) Date currentDate, @RequestParam(value = "pn", defaultValue = "1")Integer pn){
        PageHelper.startPage(pn, 7);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String date = null;
        if(currentDate != null)
            date = format.format(currentDate);
        List<CheckExtra> checkList = new ArrayList<>();
        if (date == null)
            checkList = iCheckService.selectCheckByCondition(deptId, empName, status, null, null);
        else
            checkList = iCheckService.selectCheckByCondition(deptId, empName, status, date.substring(0,4), date.substring(5));
        if(checkList.size() == 0)
            return Result.fail();
        PageInfo pageInfo = new PageInfo(checkList, 3);
        checkVOList = dataFormCheck(checkList);
        MyPageInfo<CheckVO> myPageInfo =  myPageInfo(pageInfo, checkVOList);
        return Result.success().add("myPageInfo", myPageInfo);
    }

    //员工查看自己的出勤信息,分页10条数据
    //或条件查看自己的出勤信息
    @GetMapping("/check/listCheckByEmpId")
    @ResponseBody
    @ApiOperation("员工查看自己的出勤信息,或条件查看自己的出勤信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "empId", paramType = "query", required = true, value = "员工id", dataType = "Integer"),
            @ApiImplicitParam(name = "pn", paramType = "query", value = "页码", dataType = "Integer"),
            @ApiImplicitParam(name = "status", paramType = "query", value = "出勤状态", dataType = "java.lang.Integer")
    })
    public Result listCheckByEmpId(@RequestParam(value = "empId", required = true) Integer empId, @RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value="status", defaultValue = "-1") Integer status){
//        int empId = accountService.selectEmployeeByEmpNum(empNum).getEmpId();
        List<CheckExtra> checkList = null;
        PageHelper.startPage(pn, 7);
        if(status == -1)
            checkList = iCheckService.selectPersonCheckWithName(empId);
        else
            checkList = iCheckService.selectPersonCheckByStatus(empId, status);
        if(checkList.size() == 0)
            return Result.fail();

        PageInfo pageInfo = new PageInfo(checkList, 3);
        checkVOList = dataFormCheck(checkList);
        MyPageInfo<CheckVO> myPageInfo =  myPageInfo(pageInfo, checkVOList);
        return Result.success().add("myPageInfo", myPageInfo);
    }

    //    //员工查看自己的加班记录
//    //或条件查看自己的加班记录
    @GetMapping("/check/listPersonOvertime")
    @ResponseBody
    @ApiOperation("员工查看自己的加班记录,或条件查看自己的加班记录")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "empId", paramType = "query", required = true, value = "员工id", dataType = "Integer"),
            @ApiImplicitParam(name = "pn", paramType = "query", value = "页码", dataType = "Integer")
//            @ApiImplicitParam(name = "status", paramType = "query",  value = "出勤状态", dataType = "Integer")
    })
    public Result listPersonOvertime(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "empId", required = true) Integer empId/*, @RequestParam(value="status", defaultValue = "-1") int status*/){
//        int empId = accountService.selectEmployeeByEmpNum(empNum).getEmpId();
        PageHelper.startPage(pn, 7);
        List<CheckExtra> checkList = iCheckService.selectPersonOvertime(empId);
        if(checkList.size() == 0)
            return Result.fail();
        PageInfo pageInfo = new PageInfo(checkList, 6);
        checkVOList = dataFormCheck(checkList);
        MyPageInfo<CheckVO> myPageInfo =  myPageInfo(pageInfo, checkVOList);
        return Result.success().add("myPageInfo", myPageInfo);
    }

    @ApiOperation("管理员：统计某月出勤信息")
    @GetMapping("/api/check/countByStatus")
    public Result countByStatus(@RequestParam(value = "date") Date date){
        System.out.println("riqi:"+date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        //String YEAR_MONTH = sdf.format(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        calendar.add(Calendar.MONTH,1);

        Date date2 = calendar.getTime();
        String YEAR_MONTH = sdf.format(date2);

        System.out.println("日期："+YEAR_MONTH);
        List<CheckCountVO> checkCountVOList = iCheckService.countByStatus(YEAR_MONTH);
        if(checkCountVOList==null) {
            return Result.fail("没有该月数据");
        }else {
            return Result.success().add("checkStatusList",checkCountVOList);
        }
    }
    /*初始化日期*/
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request){
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        binder.registerCustomEditor(Date.class,new CustomDateEditor(dateFormat,true));
    }

}
